package io.renren.modules.generator.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.DateUtils;
import io.renren.constant.DeviceFinal;
import io.renren.modules.generator.dao.DeviceHourDao;
import io.renren.modules.generator.entity.DeviceHourEntity;
import io.renren.modules.generator.entity.DeviceTianruiWaterEntity;
import io.renren.modules.generator.service.DeviceCombalarmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhangxu
 * @create 2020/10/12
 * Description:
 */
@Service
@Slf4j
public class DeviceCombalarmServiceImpl implements DeviceCombalarmService {

    @Autowired
    private DeviceHourDao deviceHourDao;

    @Override
    public Object chartlist(String dateStr) {
        JSONObject jsonObject = new JSONObject(true);
        try {
            DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = fmt.parse(dateStr);
            JSONArray jsonArray1;
            DeviceHourEntity deviceHourEntity;
            JSONArray jsonArray2;
            for (int j = 0; j < DeviceFinal.device_combalarm_code_arr.length; j++) {
                jsonArray1 = new JSONArray();
                JSONObject jsonObject2 = new JSONObject();
                for(Map.Entry<String, String> entry : DeviceFinal.combalarmQuotaMap.entrySet()){
                    jsonArray2 = new JSONArray();
                    String mapKey = entry.getKey();
                    String mapValue = entry.getValue();
                    Calendar calendarZero = Calendar.getInstance();
                    calendarZero.setTime(now);
                    calendarZero.set(Calendar.HOUR_OF_DAY, 0);
                    calendarZero.set(Calendar.MINUTE, 0);
                    calendarZero.set(Calendar.SECOND, 0);
                    Date zero = calendarZero.getTime();
                    QueryWrapper<DeviceHourEntity> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("device_code",mapKey)
                            .eq("create_time",zero)
                    ;
                    deviceHourEntity = deviceHourDao.selectOne(queryWrapper);
                    if(Objects.isNull(deviceHourEntity)){
                        jsonArray2.add("0.00");
                    }else{
                        jsonArray2.add(deviceHourEntity.getDeviceValue());
                    }
//                    Date day=new Date();
//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
                    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    String s = df.format(now);
                    SimpleDateFormat hourFormat=new SimpleDateFormat("HH");
//                    Date date = fmt.parse(dateStr);
                    Date date = zero;
                    GregorianCalendar calendar = new GregorianCalendar();
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    for (int i = 0; i < DateUtils.getHour(dateStr); i++) {//从1点开始到当前时间的小时
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(date);
                        cal.add(Calendar.HOUR, 1);
                        date = cal.getTime();
                        String s1 = format.format(date);
                        log.info("--------"+s1+"---------");
                        queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("device_code",mapKey)
                                .eq("create_time",date);
                        deviceHourEntity = deviceHourDao.selectOne(queryWrapper);
                        if(Objects.isNull(deviceHourEntity)){
                            jsonArray2.add("0.00");
                        }else{
                            jsonArray2.add(deviceHourEntity.getDeviceValue());
                        }
                    }
                    jsonObject2.put(mapKey,jsonArray2);
                }
                jsonArray1.add(jsonObject2);
                jsonObject.put(DeviceFinal.device_combalarm_code_arr[j],jsonArray1);
            }

        }catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}
